package leetCode.secondWeek;

public class Code_48 {
    public void rotate(int[][] matrix) {
        int x_end;
        if (matrix.length % 2 == 0) {
            x_end = matrix.length / 2;
        } else {
            x_end = (matrix.length - 1) / 2;
        }
        for (int i = 0; i < x_end; i++) {
            for (int j = matrix.length - 1 - i; j >= 1 + i; j--) {
                swap(matrix, j, i);
            }
        }
//        System.out.println("OK");
    }

    /**
     * *  *  *  *  *  *
     * *| *  *  *  *  *
     * *| *| *  *  *  *
     * *| *| *| *  *  *
     * *| *| *  *  *  *
     * *| *  *  *  *  *
     * <p>
     * *****************
     * *  *  *  *  *  *  *
     * *| *  *  *  *  *  *
     * *| *| *  *  *  *  *
     * *| *| *| *  *  *  *
     * *| *| *| *  *  *  *
     * *| *| *  *  *  *  *
     * *| *  *  *  *  *  *
     * <p>
     * 4-0 > 0-0 > 0-4 > 4-4
     * 3-1 > 1-1 > 1-3 > 3-3
     * 1-0 > 0-5 > 5-6 > 6-1
     * 6-1 > 1-0 > 0-5 > 5-6
     * x-y
     * 5-1 > 1-1 > 1-5 > 5-5
     * <p>
     * 4-0 > 3>1 > 2-2
     */
    public void swap(int[][] matrix, int x, int y) {
        int i = matrix.length - 1 - x;//记录到底的距离
        int j = matrix.length - 1 - y;

        int tmp = matrix[x][y];
        matrix[x][y] = matrix[j][x];
        matrix[j][x] = matrix[i][j];
        matrix[i][j] = matrix[y][i];
        matrix[y][i] = tmp;
    }
}
